layui.define(['table', 'form', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , admin = layui.admin;

    //排序
    table.on("sort(LAY-order-manage)", function (obj) {
        table.reload("LAY-order-manage", {
            initSort: obj,
            where: {
                field: obj.field, //排序字段   在接口作为参数字段  field order
                order: obj.type, //排序方式   在接口作为参数字段  field order
            },
            parseData: function (res) {
                // 这个地方加这个是因为 直接排序会错乱有问题，排序不准，
                $.each(res.data, function (i, key, value) {
                    res.data[i].amount = parseFloat(res.data[i].amount);
                    res.data[i].pay_amount = parseFloat(res.data[i].pay_amount);
                    res.data[i].co_amount = parseFloat(res.data[i].co_amount);
                    res.data[i].gold = parseFloat(res.data[i].gold);
                    res.data[i].gold_added = parseFloat(res.data[i].gold_added);
                });
            }
        });
    });

    //订单列表
    table.render({
        elem: '#LAY-order-manage'
        , url: '/admin/order/getlist'
        , totalRow: true
        , cols: [[
            {field: 'id', width: 80, title: 'ID', sort: true, fixed: 'left', totalRowText: '共计'}
            , {field: 'order_no', title: '订单号', width: 200, totalRowText: ''}
            , {
                field: '', title: '用户', width: 200, totalRowText: '', templet: function (d) {
                    return d.user ? d.user.nick_name + "(" + d.user.id + ")" : '';
                }
            }
            , {
                field: '', width: 160, title: '订单类型', totalRowText: '', templet: function (d) {
                    switch (d.type) {
                        case 0:
                            return "金币充值";
                        case 1:
                            return "开通贵族-vip" + d.vip_level;
                        default:
                            return "其他";
                    }
                }
            }
            , {field: 'amount', title: '订单金额/元', width: 140, sort: true, totalRowText: ''}
            , {field: 'pay_amount', title: '应付金额/元', width: 140, sort: true}
            , {
                field: 'co_amount', title: '公司实得金额/元', width: 155, sort: true, templet: function (d) {
                    if (d.co_amount) {
                        return d.co_amount;
                    }

                    return 0;
                }
            }
            , {field: 'gold', title: '购买金币', width: 120, sort: true}
            , {field: 'gold_added', title: '赠送金币', width: 120, sort: true}
            , {field: 'out_trade_no', minWidth: 160, title: '支付单号'}
            , {
                field: 'pay_channel', width: 100, title: '支付渠道', templet: function (d) {
                    switch (d.pay_channel) {
                        case 1:
                            return "微信支付";
                        case 2:
                            return "支付宝";
                        case 3:
                            return "Apple Pay";
                        case 5:
                            return "管理员";
                        default:
                            return "其他";
                    }
                }
            }
            , {
                field: '', width: 100, title: '设备类型', templet: function (d) {
                    switch (d.terminal) {
                        case 1:
                            return "IOS";
                        case 2:
                            return "Android";
                        case 3:
                            return "PC";
                        default:
                            return "";
                    }
                }
            }
            , {field: 'create_time', width: 170, title: '下单时间'}
            , {
                field: 'pay_status', width: 100, title: '支付状态', templet: function (d) {
                    switch (d.pay_status) {
                        case 0:
                            return '<span class="layui-badge layui-bg-orange">等待支付</span>';
                        case 1:
                            return '<span class="layui-badge layui-bg-green">支付成功</span>';
                        case 2:
                            return '<span class="layui-badge layui-bg-black">支付取消</span>';
                        case 3:
                            return '<span class="layui-badge layui-bg-red">支付失败</span>';
                        default:
                            return "";
                    }
                }
            }
            , {
                field: '', width: 100, align: 'center', title: '订单状态', templet: function (d) {
                    if (d.pay_status != 1) {
                        return '-';
                    }

                    if (d.is_manual == 0) {
                        return '系统到账';
                    }

                    if (d.is_manual == 1) {
                        return '人工到账';
                    }

                    return '-';
                }
            }
            , {field: 'operator', align: 'center', width: 130, title: '操作人'}
            , {title: '操作', width: 200, align: 'center', fixed: 'right', toolbar: '#table-orderlist-operation'}
        ]]
        //回调显示统计数据
        , done: function (res, curr, count) {
            if (res.statis) {
                this.elem.next().find('.layui-table-total td[data-field="order_no"] .layui-table-cell').text('订单/元：' + res.statis.count_total_amount);
                this.elem.next().find('.layui-table-total td[data-field="2"] .layui-table-cell').text('实收/元：' + res.statis.count_amount);
                this.elem.next().find('.layui-table-total td[data-field="3"] .layui-table-cell').text('订单/笔：' + res.statis.count_total);
                this.elem.next().find('.layui-table-total td[data-field="amount"] .layui-table-cell').text('成功率：' + res.statis.count_rate + '%');

            }
        }
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: {
            none: '暂无相关数据！'//默认无数据
        }
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-order-manage)', function (obj) {
        if (obj.event === 'del') {
            layer.confirm('确定删除订单？', function (index) {
                admin.req({
                    url: '/admin/order/del_post'
                    , data: {"id": obj.data.id}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-order-manage");
                            layer.msg(res.msg, {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
            });
        } else if (obj.event === 'operation') {
            layer.open({
                type: 2
                , title: '设为已支付'
                , content: '/admin/order/edit?id=' + obj.data.id
                , maxmin: true
                , area: ['500px', '400px']
            });
        }
    });

    form.on('submit(order-add)', function (obj) {
        //提交
        admin.req({
            url: '/admin/order/add_post'
            , data: obj.field
            , success: function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    setTimeout(function () {
                        window.location.reload();
                    }, 1200);
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }
        });
        return false;
    });

    // 统计
    table.render({
        elem: '#LAY-order-statis'
        , url: '/admin/order/statis'
        , cols: [[
            {
                field: '', title: '今日订单<i class="layui-icon layui-icon-help statisTips" data-type="1"></i>', style: 'height:50px', templet: function (d) {
                    let html = `<div class="statis_area">
                            <p>
                                订单/元：${d.today_total_amount} </br>
                                实收/元：<span style="color:#FF5722">${d.today_amount}</span>
                            </p>
                           <p>
                                订单/笔：${d.today_total}</br>
                                成功/笔：${d.today_success_total}</br>
                                成功率： <span style="color:#5FB878">${d.today_rate}%</span>
                            </p>
                       </div>`
                    return html;
                }
            }
            , {
                field: '', title: '昨日订单<i class="layui-icon layui-icon-help statisTips" data-type="2"></i>', templet: function (d) {
                    let html = `<div class="statis_area">
                            <p>
                                订单/元：${d.yesterday_total_amount} </br>
                                实收/元：<span style="color:#FF5722">${d.yesterday_amount}</span>
                            </p>
                           <p>
                                订单/笔：${d.yesterday_total}</br>
                                成功/笔：${d.yesterday_success_total}</br>
                                成功率： <span style="color:#5FB878">${d.yesterday_rate}%</span>
                            </p>
                       </div>`
                    return html;
                }
            }
            // , {
            //     field: '', title: '前7日订单<i class="layui-icon layui-icon-help statisTips" data-type="3"></i>', templet: function (d) {
            //         let html = `<div class="statis_area">
            //                 <p>
            //                     订单/元：${d.sevenday_total_amount} </br>
            //                     实收/元：<span style="color:#FF5722">${d.sevenday_amount}</span>
            //                 </p>
            //                <p>
            //                     订单/笔：${d.sevenday_total}</br>
            //                     成功/笔：${d.sevenday_success_total}</br>
            //                     成功率： <span style="color:#5FB878">${d.sevenday_rate}%</span>
            //                 </p>
            //            </div>`
            //         return html;
            //     }
            // }
            , {
                field: '', title: '本月订单<i class="layui-icon layui-icon-help statisTips" data-type="4"></i>', templet: function (d) {
                    let html = `<div class="statis_area">
                            <p>
                                订单/元：${d.thismonth_total_amount} </br>
                                实收/元：<span style="color:#FF5722">${d.thismonth_amount}</span>
                            </p>
                           <p>
                                订单/笔：${d.thismonth_total}</br>
                                成功/笔：${d.thismonth_success_total}</br>
                                成功率： <span style="color:#5FB878">${d.thismonth_rate}%</span>
                            </p>
                       </div>`
                    return html;
                }
            }
            , {
                field: '', title: '上月订单<i class="layui-icon layui-icon-help statisTips" data-type="5"></i>', templet: function (d) {
                    let html = `<div class="statis_area">
                            <p>
                                订单/元：${d.lastmonth_total_amount} </br>
                                实收/元：<span style="color:#FF5722">${d.lastmonth_amount}</span>
                            </p>
                           <p>
                                订单/笔：${d.lastmonth_total}</br>
                                成功/笔：${d.lastmonth_success_total}</br>
                                成功率： <span style="color:#5FB878">${d.lastmonth_rate}%</span>
                            </p>
                       </div>`
                    return html;
                }
            }
        ]]
        , text: '对不起，加载出现异常！'
        , even: true
    });


    // // 鼠标经过气泡显示
    var tips;
    $(document).on({
        mouseenter: function () {
            var that = this;
            let type = $(this).data('type');
            let tips_msg = '';
            switch (type) {
                case 1:
                    tips_msg = '统计今日00:00:00后至当前数据';
                    break;
                case 2:
                    tips_msg = '统计昨日00:00:00后至23:59:59数据';
                    break;
                case 3:
                    tips_msg = '统计截止到当前7日数据';
                    break;
                case 4:
                    tips_msg = '统计本月1日至当前的数据';
                    break;
                case 5:
                    tips_msg = '统计上个自然月1日至月末最后一天的数据';
                    break;
            }
            if (tips_msg) {
                tips = layer.tips(tips_msg, that, {tips: 3});
            }
        },
        mouseleave: function () {
            layer.close(tips);
        }
    }, '.statisTips');

    exports('order', {})
});